package com.antwork.common.util;

import java.io.PrintStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtility {
	public static final char[] ascchars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
			'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
			'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
			'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
			'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', '0', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*',
			'(', ')', '-', '_', '=', '+', '|', '\\', '[', ']', '{', '}', '\'',
			'"', ';', ':', ',', '.', '<', '>', '/', '?' };

	public static String generateUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replaceAll("-", "").toLowerCase();
	}

	public static String getNotNullValue(String str, String defaultValue) {
		if (str != null) {
			return str;
		}
		return defaultValue;
	}

	public static boolean isEmail(String email) {
		if ((email == null) || ("".equals(email))) {
			return false;
		}

		Pattern p = Pattern
				.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		Matcher m = p.matcher(email);
		return m.matches();
	}

	public static String getMoreString(String str, int length, String more) {
		if ((str == null) || (str.equals(""))) {
			return "";
		}
		int len = str.length();
		int curLength = 0;
		StringBuffer buf = new StringBuffer();

		boolean hasMore = false;
		for (int i = 0; i < len; ++i) {
			boolean isSingleChar = false;
			if (curLength < length) {
				for (int j = 0; j < ascchars.length; ++j) {
					if (str.charAt(i) == ascchars[j]) {
						buf.append(str.charAt(i));
						++curLength;
						isSingleChar = true;
						break;
					}
				}
				if (!isSingleChar) {
					buf.append(str.charAt(i));
					curLength += 2;
				}
			} else {
				hasMore = true;
			}
		}
		if (more == null) {
			more = "...";
		}
		if (hasMore) {
			return more;
		}
		return buf.toString();
	}

	public static void main(String[] args) {
		String str = null;
		if (isEmail(str))
			System.out.println("yes");
		else
			System.out.println("no");
	}
}